package com.carefree.bookmall.controller;

import com.carefree.bookmall.common.utils.AesEncryptUtil;
import com.carefree.bookmall.common.utils.Constant;
import com.carefree.bookmall.entity.User;
import com.carefree.bookmall.exception.CustomException;
import com.carefree.bookmall.service.ShortMessageService;
import com.carefree.bookmall.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName:UserController
 * @Description:
 * @Author Tengbin
 */
@Controller
@RequestMapping("/bookmall")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ShortMessageService shortMessageService;


    /**
     * 用户注册提交
     *
     * @return
     */
    @PostMapping("/register")
    public String registerUserSubmit(User user, String authcode, HttpServletRequest request) throws Exception {

        boolean flag = shortMessageService.compareSmsCode(user.getPhone(), authcode);

        if (flag) {
            if (userService.register(user)) {
                request.setAttribute(Constant.MESSAGE, Constant.REGISTER_MESSAGE_SUCCESS);
                return "/login";
            } else {
                request.setAttribute(Constant.FAIL_MESSAGE, Constant.REGISTER_MESSAGE_FAIL);
            }
        } else {
            request.setAttribute(Constant.FAIL_MESSAGE, Constant.REGISTER_ERROR_CODE);
        }
        return "register";
    }


    /**
     * 用户登录提交
     *
     * @param tempUser 用户登录信息
     * @param request  请求对象
     * @param session  当前会话对象
     * @return
     */
    @PostMapping("/login")
    public String userLogin(User tempUser, HttpServletRequest request, HttpSession session) throws Exception {
        // 根据客户端用户名查询是否存在该用户
        User user = userService.login(tempUser.getUserName());
        if (ObjectUtils.isEmpty(user)) {
            request.setAttribute(Constant.FAIL_MESSAGE, Constant.LOGIN_MESSAGE_NONE);
            return "login";
        }

        String desEncrypt = AesEncryptUtil.getInstance().desEncrypt(tempUser.getPassword());

        // 判断客户端输入的密码是否与数据库的密码相同
        if (AesEncryptUtil.getInstance().desEncrypt(user.getPassword()).equals(desEncrypt)) {
            user.setPassword("");
            session.setAttribute(Constant.SESSION_USER, user);
            return "redirect:/bookmall/index";
        }
        request.setAttribute(Constant.FAIL_MESSAGE, Constant.LOGIN_MESSAGE_FAIL);
        return "login";
    }


    /**
     * 修改收货信息
     *
     * @param userId   用户id
     * @param realName 真实姓名
     * @param phone    手机号
     * @param address  地址
     * @param request
     * @return
     */
    @PostMapping("/user/info")
    public String updateUserInformation(Integer id, String realName, String phone, String address, HttpServletRequest request) throws CustomException {

        boolean flag = userService.updatePhoneAndAddress(id, realName, phone, address);

        if (flag) {
            request.setAttribute(Constant.MESSAGE, Constant.UPDATE_MESSAGE_SUCCESS);
            // session中的user对象
            User sessionUser = (User) request.getSession().getAttribute(Constant.SESSION_USER);
            sessionUser.setRealName(realName);
            sessionUser.setPhone(phone);
            sessionUser.setAddress(address);
            // 先移除原先session存储的user信息，再重新设值
            request.getSession().removeAttribute(Constant.SESSION_USER);
            request.getSession().setAttribute(Constant.SESSION_USER, sessionUser);
        } else {
            request.setAttribute(Constant.FAIL_MESSAGE, Constant.UPDATE_MESSAGE_FAIL);
        }

        return "user_center";
    }


    /**
     * 修改密码
     *
     * @param id          userId
     * @param oldpassword 旧密码
     * @param newpassword 新密码
     * @param request
     * @return
     */
    @PostMapping("/user/safe")
    public String updateUserPassword(Integer id, String oldpassword, String newpassword, HttpServletRequest request) throws Exception {
        // 执行db修改密码
        boolean flag = userService.updatePassword(id, oldpassword, newpassword);

        if (flag) {
            request.setAttribute(Constant.MESSAGE, Constant.UPDATE_MESSAGE_SUCCESS);
        } else {
            request.setAttribute(Constant.FAIL_MESSAGE, Constant.UPDATE_MESSAGE_FAIL);
        }
        return "user_center";
    }


    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute(Constant.SESSION_USER);
        request.getSession().removeAttribute(Constant.SESSION_ORDER);
        return "redirect:/bookmall/index";
    }


}
